﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using Newtonsoft.Json;

namespace ZLCMS.Web.SystemManage.Basics.FormInfo
{
    public partial class FormField : System.Web.UI.Page
    {
        protected string JsonData { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["FormId"] = Int32.Parse(Request["formid"]);
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    JsonData = JsonConvert.SerializeObject(session.CreateQuery("from SysFormField where FormId=:fid order by Sort")
                        .SetParameter("fid", ViewState["FormId"])
                        .List<SysFormField>());
                }
            }
            else
            {
                int formId = (int)ViewState["FormId"];
                IList<SysFormField> srcList = JsonConvert.DeserializeObject<IList<SysFormField>>(Request["json_data"]);
                using(ISession session = SessionFactoryHelper.CreateSession())
                {
                    //已存在的id
                    IList<int> existIds = new List<int>();

                    foreach(SysFormField item in srcList)
                    {
                        SysFormField target = null;
                        if (item.Id > 0)
                        {
                            target = session.Get<SysFormField>(item.Id);
                        }
                        if (target == null)
                        {
                            target = new SysFormField();
                        }
                        target.ControlType = item.ControlType;
                        target.DataSource = item.DataSource;
                        target.FieldName = item.FieldName;
                        target.FormId = formId;
                        target.Memo = item.Memo;
                        target.Sort = item.Sort;
                        target.ValidatorConfig = item.ValidatorConfig;

                        session.SaveOrUpdate(target);

                        existIds.Add(target.Id);
                    }

                    //删除不存在的表单项目
                    session.CreateSQLQuery("delete from sys_form_field where recid not in (:ids) and form_id=:fid")
                        .SetParameter("fid", formId)
                        .SetParameterList("ids", existIds.ToArray())
                        .ExecuteUpdate();

                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/FormInfo/FormField.aspx?tabid=FormField&formid=" + ViewState["FormId"];



                _framePage.Alert(rc);
            }
        }
    }
}